<?php 
class Study_model extends CI_Model
{
	var $member=array();
	public function __construct()
	{
		$this->member[0]='student';
		$this->member[1]='teacher';
		$this->member[2]='educator';
		$this->member[3]='parent';
		$this->member[4]='institute';
		$this->email_setting = $this->lang->line('email');
	}
	
	public function insertUserVisitData($value=NULL)
	{
		$data = $this->session->userdata('study_data');

		$date=date('Y-m-d H:i:s');
		$activation=md5($date.$value['email']);
		
		$value['activation']=$activation;
		$value['active']='N';
		$value['created_date']=$date;
		$value['modified_date']=$date;
			
		$mem_id=$this->common_model->insert_data('members', $value);
		
		$class['member_id_fk'] = htmlspecialchars(strip_tags($mem_id),ENT_QUOTES);
		$class['class_id_fk'] = htmlspecialchars(strip_tags($data['class']),ENT_QUOTES);
		$class['class_name'] = htmlspecialchars(strip_tags('Class '.$data['class']),ENT_QUOTES);
		$class['created_date'] = htmlspecialchars(strip_tags($date),ENT_QUOTES);
		$class['modified_date'] = htmlspecialchars(strip_tags($date),ENT_QUOTES);
		$class['status'] = htmlspecialchars(strip_tags('Active'),ENT_QUOTES);
		
		$class_id=$this->common_model->insert_data('member_classes', $class);
		
		$subject['member_id_fk'] = htmlspecialchars(strip_tags($mem_id),ENT_QUOTES);
		$subject['subject_name'] = htmlspecialchars(strip_tags($data['subject_1']),ENT_QUOTES);
		$subject['subject_id_fk'] = htmlspecialchars(strip_tags($data['subject']),ENT_QUOTES);
		$subject['class_id_fk'] = htmlspecialchars(strip_tags($data['class']),ENT_QUOTES);
		$subject['created_date'] = htmlspecialchars(strip_tags($date),ENT_QUOTES);
		$subject['status'] = htmlspecialchars(strip_tags('Active'),ENT_QUOTES);
		
		$subject_id=$this->common_model->insert_data('member_subjects', $subject);
		
		$book['member_id_fk'] = htmlspecialchars(strip_tags($mem_id),ENT_QUOTES);
		$book['book_name'] = htmlspecialchars(strip_tags($data['book_full']),ENT_QUOTES);
		$book['book_id_fk'] = htmlspecialchars(strip_tags($data['book']),ENT_QUOTES);
		$book['book_rating'] = htmlspecialchars(strip_tags('5'),ENT_QUOTES);
		$book['created_date'] = htmlspecialchars(strip_tags($date),ENT_QUOTES);
		$book['status'] = htmlspecialchars(strip_tags('Active'),ENT_QUOTES);
		
		$book_id=$this->common_model->insert_data('member_books', $book);
		
		return $mem_id;
	}
	
	public function updateUserVisitData($value=NULL, $id=NULL, $type=NULL)
	{
		$date=date('Y-m-d H:i:s');
		if($type == 'school')
		{
			$value1['member_id_fk'] = $id;
			if(isset($value['school1']) && $value['school1']!='')
			{
				$value1['school_id_fk'] = $value['school1'];
			}
			else {
				$value1['school_id_fk'] = NULL;
			}
			$value1['school_name'] = $value['school1_full'];
			$value1['board_id_fk'] = 'CBSE';
			$value1['board_name'] = 'Central Board of Secondary Education';
			$value1['modified_date']=$date;
			$value1['created_date']=$date;
			$value1['status'] = 'Active';
			
			$ref = $query = $this->db->insert($this->db->dbprefix('member_schools'),$value1);
			return $ref;
		}
		else if($type == 'section')
		{
			$value3['modified_date']=$date;
			$value3['section'] = $value['section1'];
					
			$this->db->where('member_id_fk',$id);
			$query = $this->db->update($this->db->dbprefix('member_schools'),$value3);
			
			if(isset($value['category']))
			{
				if($value['category'] == 'male')
				$value4['gender'] = 'M';
				else if($value['category'] == 'female')
				$value4['gender'] = 'F';
				else if($value['category'] == 'other')
				$value4['gender'] = 'O';
				
				$this->db->where('member_id',$id);
				$query = $this->db->update($this->db->dbprefix('members'),$value3);
			}
		}
		else {
			$value['modified_date']=$date;
		
			$this->db->where('member_id',$id);
			$query = $this->db->update($this->db->dbprefix('members'),$value);
		}
	}
	
	public function verify_UserVisitName($value=NULL, $id=NULL)
	{
		if (empty($value))
		{
			return FALSE;
		}
				
		$sql_select = array('*');
		$sql_whrere=array('first_name'=>$value['first_name1'],'last_name'=>$value['last_name1'], 'member_id'=>$id);
		$query = $this->db->select($sql_select)->where($sql_whrere)->get($this->db->dbprefix('members'));		 
	    $resultData = array();
		if($query->num_rows() > 0)
		{
			$resultData = $query->result_array();
		}
		return $resultData;
	}

	public function verify_UserVisitSchool($value=NULL)
	{
		if (empty($value))
		{
			return FALSE;
		}
		
		$sql_select = array('*');
		$sql_whrere=array('member_id_fk'=>$value[0]['member_id']);
		$query = $this->db->select($sql_select)->where($sql_whrere)->get($this->db->dbprefix('member_schools'));		 
	    $resultData = array();
		if($query->num_rows() > 0)
		{
			$resultData = $query->result_array();
		}
		return $resultData;
	}
	
	public function verify_UserVisitSection($value=NULL, $section=NULL)
	{
		if (empty($value))
		{
			return FALSE;
		}
		
		$sql_select = array('*');
		$sql_whrere=array('member_id_fk'=>$value[0]['member_id'], 'section'=>strtolower($section));
		$query = $this->db->select($sql_select)->where($sql_whrere)->get($this->db->dbprefix('member_schools'));	 
                $resultData = array();
		if($query->num_rows() > 0)
		{
			$resultData = $query->result_array();
		}
		return $resultData;
	}
	
	public function getDetailByMail($value='')
	{
		if (empty($value))
		{
			return FALSE;
		}
		
		$sql_select = array('member_id');
		$sql_whrere=array('email'=>$value[0]['email']);
		$query = $this->db->select($sql_select)->where($sql_whrere)->get($this->db->dbprefix('members'));	 
                $resultData = array();
		if($query->num_rows() > 0)
		{
			$resultData = $query->result_array();
		}
		return $resultData;
	}
	
	
	public function getChapterDetail($study_data, $type="")
	{
		if (empty($study_data))
		{
			return FALSE;
		}
		
		$sql_select = "(q.question_mcq_seq_id), (q.question_topic), (q.question_type), (q.question_description), (c.chapter_title)";
		$sql_where="(q.question_mcq_seq_id=c.question_id) AND c.chapter_number = ".$study_data['chapter']." AND c.book_id = '".$study_data['book']."' AND c.subject_id = '".$study_data['subject']."' AND c.class_id = '".$study_data['class']."'";
		if($type == 'ALL' || $type == ''){}
		else {
			$sql_where .= " AND q.question_type =  '".$type."'";
		}
		$this->db->where($sql_where);
		$query = $this->db->select($sql_select)->order_by('(c.creation_date) asc')->limit(1)->get($this->db->dbprefix('questions q, chapter_masters c'));
		//echo $this->db->last_query();die();
		$resultData = $query->result_array();
		return $resultData;
	}
	
	
######################## 	Analytics, Que Share and Floater bar code starts here   #############################

	public function get_question_access_counts($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('*');
		$sql_where = 'member_id = ' . $member_id ;
		$query=$this->db->select($sql_select)->where($sql_where)->get($this->db->dbprefix('question_access_log'));
	    return $query->num_rows();
	}
	
	public function get_question_access_counts_graph_weekly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('created_date', 'COUNT(WEEK(created_date)) as total_counts', 'WEEK(created_date) as weeks');
		$sql_where = "member_id = '".$member_id."'";
		$query=$this->db->select($sql_select)->where($sql_where)->group_by('WEEK(created_date)')->order_by('WEEK(created_date)', 'DESC')->limit(4)->get($this->db->dbprefix('question_access_log'));
	    $result = $query->result_array();
		//print_r($result);
		return $result;
//		->limit(4,3)
	}
		
	public function get_question_access_counts_graph_monthly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('created_date', 'COUNT(MONTH(created_date)) as total_counts', 'MONTH(created_date) as months');
		$sql_where = "member_id = '".$member_id."'";
		$query=$this->db->select($sql_select)->where($sql_where)->group_by('MONTH(created_date)')->order_by('MONTH(created_date)', 'DESC')->limit(4)->get($this->db->dbprefix('question_access_log'));
	    $result = $query->result_array();
		//print_r($result);
		return $result;
//		->limit(4,3)
	}
		
	public function set_question_access_counts($question_id, $data)
	{
		$member_id = $this->session->userdata('id');
		$access_counts = $this->get_question_access_counts($question_id);
		$date=date('Y-m-d H:i:s');
		
		$sql_insert['question_mcq_seq_id'] = $question_id;
		$sql_insert['member_id'] = $member_id;
		$sql_insert['created_date'] = $date;
		$sql_insert['question_access_from_'.$data['type'].''] = 1;
		
		$query = $this->db->insert($this->db->dbprefix('question_access_log'), $sql_insert);
		if($query)
		{
			return "Success";
		}
	}
	
	public function get_question_liked_counts($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('*');
		$sql_where = 'member_id = ' . $member_id . ' AND question_like_status="Yes"';
		$query=$this->db->select($sql_select)->where($sql_where)->get($this->db->dbprefix('question_like_log'));
	    //echo $query->num_rows();
	    return $query->num_rows();
	}

	public function get_question_liked_counts_graph_weekly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('created_date', 'COUNT(WEEK(created_date)) as total_counts', 'WEEK(created_date) as weeks');
		$sql_where = "member_id = " . $member_id . " AND question_like_status = 'Yes'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('WEEK(created_date)', 'DESC')->group_by('WEEK(created_date)')->limit(4)->get($this->db->dbprefix('question_like_log'));
	    $result = $query->result_array();
		return $result;
	}
	
	public function get_question_liked_counts_graph_monthly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('created_date', 'COUNT(MONTH(created_date)) as total_counts', 'MONTH(created_date) as months');
		$sql_where = "member_id = " . $member_id . " AND question_like_status = 'Yes'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('MONTH(created_date)', 'DESC')->group_by('MONTH(created_date)')->limit(4)->get($this->db->dbprefix('question_like_log'));
	    $result = $query->result_array();
		return $result;
	}
	
	public function set_question_liked_counts($question_id, $data)
	{
		$member_id = $this->session->userdata('id');
		$date=date('Y-m-d H:i:s');
		
		$sql_select1 = array('*');
		$this->db->select($sql_select1);
		$this->db->where('member_id', $member_id);
		if(@$question_id)
		{
			$this->db->where('question_mcq_seq_id', $question_id);
		}
		$query1= $this->db->get($this->db->dbprefix('question_like_log'));
	    
		if( ! $query1->num_rows() > 0)
		{
			$sql_insert['question_mcq_seq_id'] = $question_id;
			$sql_insert['member_id'] = $member_id;
			$sql_insert['question_like_status'] = 'Yes';
			$sql_insert['created_date'] = $date;
			$query = $this->db->insert($this->db->dbprefix('question_like_log'), $sql_insert);			
		}
		else
		{
			$query = $this->db->query("UPDATE ". $this->db->dbprefix('question_like_log') ." SET question_like_status='Yes' WHERE member_id=". $member_id ." AND question_mcq_seq_id='". $question_id ."'");			
		}
		
		if($query)
		{
			return "Success";
		}
	}
	
	public function get_question_shared_counts($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('question_shared_platform', 'count(question_shared_platform) as shared_count');
		$sql_where = 'member_id = ' . $member_id;
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('question_shared_platform', 'ASC')->group_by('question_shared_platform')->get($this->db->dbprefix('question_shared_log'));
	    $result = $query->result_array();
		return $result;
	}
	
	public function get_question_shared_counts_graph_weekly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('SUM(IF(`question_shared_platform` = "twitter",1,0)) as twitter', 'SUM(IF(`question_shared_platform` = "linkedin",1,0)) as linkedin', 'SUM(IF(`question_shared_platform` = "facebook",1,0)) as facebook', 'WEEK(created_date) as weeks');
		$sql_where = "member_id = '".$member_id."'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('WEEK(created_date)', 'DESC')->group_by('WEEK(created_date)')->limit(4)->get($this->db->dbprefix('question_shared_log'));

	    $result = $query->result_array();
		//echo $this->db->last_query();
		//print_r($result);
		return $result;
	}
	
	public function get_question_shared_counts_graph_monthly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('SUM(IF(`question_shared_platform` = "twitter",1,0)) as twitter', 'SUM(IF(`question_shared_platform` = "linkedin",1,0)) as linkedin', 'SUM(IF(`question_shared_platform` = "facebook",1,0)) as facebook', 'MONTH(created_date) as months');
		$sql_where = "member_id = '".$member_id."'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('MONTH(created_date)', 'DESC')->group_by('MONTH(created_date)')->limit(4)->get($this->db->dbprefix('question_shared_log'));

	    $result = $query->result_array();
		//echo $this->db->last_query();
		//print_r($result);
		return $result;
	}
	
/*	public function set_question_shared_counts($question_id, $data)
	{
		$member_id = $this->session->userdata('id');
		$date=date('Y-m-d H:i:s');
		
		$sql_insert['question_mcq_seq_id'] = $question_id;
		$sql_insert['member_id'] = $member_id;
		$sql_insert['question_shared_platform']=$data['shared_platform'];
		$sql_insert['created_date'] = $date;
		
		$query = $this->db->insert($this->db->dbprefix('question_shared_log'), $sql_insert);
		if($query)
		{
			return "Success";
		}
	}
*/
	
	public function get_question_flag_counts($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('question_flag_type', 'count(question_flag_type) as flag_count');
		$sql_where = 'member_id = ' . $member_id;
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('question_flag_type', 'ASC')->group_by('question_flag_type')->get($this->db->dbprefix('question_flag_log'));
	    $result = $query->result_array();
		return $result;
	}
	
	public function get_question_flag_counts_graph_weekly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('created_date', 'COUNT(WEEK(created_date)) as total_counts', 'WEEK(created_date) as weeks');
		$sql_where = "member_id = '".$member_id."'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('WEEK(created_date)', 'DESC')->group_by('WEEK(created_date)')->limit(4)->get($this->db->dbprefix('question_flag_log'));
	    $result = $query->result_array();
		//echo $this->db->last_query();
		//print_r($result);
		return $result;
	}
	
	public function get_question_flag_counts_graph_monthly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('created_date', 'COUNT(MONTH(created_date)) as total_counts', 'MONTH(created_date) as months');
		$sql_where = "member_id = '".$member_id."'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('MONTH(created_date)', 'DESC')->group_by('MONTH(created_date)')->limit(4)->get($this->db->dbprefix('question_flag_log'));
	    $result = $query->result_array();
		//echo $this->db->last_query();
		//print_r($result);
		return $result;
	}
	
	public function set_question_flags($question_id, $data)
	{
		$member_id = $this->session->userdata('id');
		$date=date('Y-m-d H:i:s');

		$sql_insert['question_mcq_seq_id'] = $question_id;
		$sql_insert['member_id'] = $member_id;
		$sql_insert['question_flag_type']=$data['flag_type'];
		$sql_insert['question_flag_desc']=$data['flag_description'];
		$sql_insert['created_date'] = $date;
		
		$query = $this->db->insert($this->db->dbprefix('question_flag_log'), $sql_insert);
		if($query)
		{
			return "Success";
		}
	}
	
	public function get_question_download_counts($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		$sql_select = array('*');
		$sql_where = 'member_id = ' . $member_id;
		$query=$this->db->select($sql_select)->where($sql_where)->get($this->db->dbprefix('question_download_log'));
	    $result = $query->num_rows();
		return $result;
	}
	
	public function get_question_download_counts_graph_weekly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('created_date', 'COUNT(WEEK(created_date)) as total_counts', 'WEEK(created_date) as weeks');
		$sql_where = "member_id = '".$member_id."'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('WEEK(created_date)', 'DESC')->group_by('WEEK(created_date)')->limit(4)->get($this->db->dbprefix('question_download_log'));
	    $result = $query->result_array();
		return $result;
	}
	
	public function get_question_download_counts_graph_monthly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('created_date', 'COUNT(MONTH(created_date)) as total_counts', 'MONTH(created_date) as months');
		$sql_where = "member_id = '".$member_id."'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('MONTH(created_date)', 'DESC')->group_by('MONTH(created_date)')->limit(4)->get($this->db->dbprefix('question_download_log'));
	    $result = $query->result_array();
		return $result;
	}
	
	public function set_question_download_counts($question_id, $data)
	{
		$member_id = $this->session->userdata('id');
		$date=date('Y-m-d H:i:s');

		$sql_select1 = array('*');
		$sql_where1 = 'member_id = ' . $member_id . ' AND DATE_FORMAT(created_date, "%d") = DATE_FORMAT(CURDATE(), "%d")';
		$query1 = $this->db->select($sql_select1)->where($sql_where1)->get($this->db->dbprefix('question_download_log'));
	    $counts = $query1->num_rows();

		if($counts < 3)
		{			
			$sql_insert['question_mcq_seq_id'] = $question_id;
			$sql_insert['member_id'] = $member_id;
			$sql_insert['created_date'] = $date;
			$query = $this->db->insert($this->db->dbprefix('question_download_log'), $sql_insert);
			if($query)
			{
				return "Success";
			}
		}
		else
		{
			return "You have exceed the limit of download for today.";
		}
		
	}
	
	public function get_question_time_to_answer($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		//$sql_select = array('SEC_TO_TIME(AVG(TIME_TO_SEC(`question_correct_answer_time`))) as avgtime');
		$sql_select = array('FORMAT(AVG(`question_correct_answer_time`), 2) as avgtime');
		$sql_where = 'member_id = ' . $member_id . ' AND question_correct_answer > 0';
		$query=$this->db->select($sql_select)->where($sql_where)->get($this->db->dbprefix('question_answer_log'));
	    $result = $query->result();
		//echo $this->db->last_query();
		echo $result[0]->avgtime;
	}
	
	public function get_question_time_to_answer_graph_weekly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('created_date', 'question_correct_answer_time', 'FORMAT(SUM(`question_correct_answer_time`), 2) as correctanswertime', 'FORMAT(SUM(`question_wrong_answer_time`), 2) as wronganswertime', 'WEEK(created_date) as weeks');
		$sql_where = "member_id = '".$member_id."'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('WEEK(created_date)', 'DESC')->group_by('WEEK(created_date)')->limit(4)->get($this->db->dbprefix('question_answer_log'));
	    $result = $query->result_array();
		//print_r($result);
		return $result;
	}
	
	public function get_question_time_to_answer_graph_monthly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('created_date', 'question_correct_answer_time', 'FORMAT(SUM(`question_correct_answer_time`), 2) as correctanswertime', 'FORMAT(SUM(`question_wrong_answer_time`), 2) as wronganswertime', 'MONTH(created_date) as months');
		$sql_where = "member_id = '".$member_id."'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('MONTH(created_date)', 'DESC')->group_by('MONTH(created_date)')->limit(4)->get($this->db->dbprefix('question_answer_log'));
	    $result = $query->result_array();
		//print_r($result);
		return $result;
	}
	
	public function set_question_time_to_answer($question_id, $data)
	{
		$member_id = $this->session->userdata('id');
		$date=date('Y-m-d H:i:s');
			
		

		$sql_insert['question_mcq_seq_id'] = $question_id;
		$sql_insert['member_id'] = $member_id;

//		$sql_where1 = 'member_id = ' . $member_id . ' AND question_mcq_seq_id = '. $question_id;
		$sql_select1 = array('*');
		$this->db->select($sql_select1);
		$this->db->where('member_id', $member_id);
		if(@$question_id)
		{
			$this->db->where('question_mcq_seq_id', $question_id);
		}
		$query1= $this->db->get($this->db->dbprefix('question_answer_log'));

		if( ! $query1->num_rows() > 0)
		{
			$sql_insert['question_correct_answer_time']=$data['correct_answer_time'];
			$sql_insert['question_correct_answer']=$data['correct_answer'];
			$sql_insert['question_wrong_answer_time']=$data['wrong_answer_time'];
			$sql_insert['question_wrong_answer']=$data['wrong_answer'];
			$sql_insert['question_answer_time']=$data['answer_time'];
			$sql_insert['created_date'] = $date;
			$query = $this->db->insert($this->db->dbprefix('question_answer_log'), $sql_insert);			
		}
		else
		{
			$sql_insert['created_date'] = $date;
			if($data['correct_answer'])
			{
				$query = $this->db->query("UPDATE ". $this->db->dbprefix('question_answer_log') ." SET question_correct_answer = question_correct_answer+1, question_correct_answer_time = FORMAT(((question_correct_answer_time + ". $data['correct_answer_time'] .")/2),2) WHERE member_id=". $member_id ." AND question_mcq_seq_id='". $question_id ."'");
			}
			else
			{
				$query = $this->db->query("UPDATE ". $this->db->dbprefix('question_answer_log') ." SET question_wrong_answer = question_wrong_answer+1, question_wrong_answer_time = FORMAT(((question_wrong_answer_time + ". $data['wrong_answer_time'] .")/2),2) WHERE member_id=". $member_id ." AND question_mcq_seq_id='". $question_id ."'");				
			}
			$query = $this->db->insert($this->db->dbprefix('question_answer_log'), $sql_insert);
		}
		if($query)
		{
			return "Success";
		}
	}
	
	public function get_question_appearance($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('question_access_from_study', 'count(question_access_from_study) as Study', 'question_access_from_practice', 'count(question_access_from_practice) as Practice', 'question_access_from_assignment', 'count(question_access_from_assignment) as Assign', 'question_access_from_test', 'count(question_access_from_test) as Test', 'created_date');
		$sql_where = "member_id = " . $member_id . " AND question_mcq_seq_id = '". $question_id ."'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('created_date', 'ASC')->group_by('created_date')->get($this->db->dbprefix('question_access_log'));
	    $result = $query->result_array();	
		return $result;
	}
	
/*	public function get_question_appearance_graph($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('created_date', 'COUNT(DATE_FORMAT(created_date, "%Y")) as total_counts', 'DATE_FORMAT(created_date, "%Y") as years');
		$sql_where = "member_id = '".$member_id."' and question_mcq_seq_id='".$question_id."'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('years', 'ASC')->group_by('years')->get($this->db->dbprefix('question_access_log'));
	    $result = $query->result_array();
		return $result;
	}
*/	
	public function get_question_correct_answer_counts($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('*');
		$sql_where = 'member_id = ' . $member_id . ' AND question_correct_answer = 1';
		$query=$this->db->select($sql_select)->where($sql_where)->get($this->db->dbprefix('question_answer_log'));
	    $result = $query->num_rows();
		return $result;
	}
	
	public function get_question_correct_answer_counts_graph_weekly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('created_date', 'COUNT(WEEK(created_date)) as total_counts', 'WEEK(created_date) as weeks');
		$sql_where = "member_id = '".$member_id."' AND question_correct_answer = 1";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('WEEK(created_date)', 'DESC')->group_by('WEEK(created_date)')->limit(4)->get($this->db->dbprefix('question_answer_log'));
	    $result = $query->result_array();
		return $result;
	}
	
	public function get_question_correct_answer_counts_graph_monthly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('created_date', 'COUNT(MONTH(created_date)) as total_counts', 'MONTH(created_date) as months');
		$sql_where = "member_id = '".$member_id."' AND question_correct_answer = 1";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('MONTH(created_date)', 'DESC')->group_by('MONTH(created_date)')->limit(4)->get($this->db->dbprefix('question_answer_log'));
	    $result = $query->result_array();
		return $result;
	}
	
	public function get_question_wrong_answer_counts($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('*');
		$sql_where = 'member_id = ' . $member_id . ' AND question_wrong_answer = 1';
		$query=$this->db->select($sql_select)->where($sql_where)->get($this->db->dbprefix('question_answer_log'));
	    $result = $query->num_rows();
		return $result;
	}
	
	
	public function get_question_wrong_answer_counts_graph_weekly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('created_date', 'COUNT(WEEK(created_date)) as total_counts', 'WEEK(created_date) as weeks');
		$sql_where = "member_id = '".$member_id."' AND question_wrong_answer = 1";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('WEEK(created_date)', 'DESC')->group_by('WEEK(created_date)')->get($this->db->dbprefix('question_answer_log'));
	    $result = $query->result_array();
		return $result;
	}
	
	public function get_question_wrong_answer_counts_graph_monthly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('created_date', 'COUNT(MONTH(created_date)) as total_counts', 'MONTH(created_date) as months');
		$sql_where = "member_id = '".$member_id."' AND question_wrong_answer = 1";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('MONTH(created_date)', 'DESC')->group_by('MONTH(created_date)')->get($this->db->dbprefix('question_answer_log'));
	    $result = $query->result_array();
		return $result;
	}
	
	public function get_question_type_counts($questiontype_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$question_category = 'Z';
		//$sql_select = array('SEC_TO_TIME(AVG(TIME_TO_SEC(`question_correct_answer_time`))) as avgtime');
		$sql_select = "chapter_masters.question_category, questions.question_type, COUNT(zrc_questions.question_type) as qtype_counts ,question_types.description";
		
		$sql_where = 'chapter_masters.question_category = "'. $question_category .'"';
		
		$this->db->select($sql_select);
		$this->db->where('question_category','Z');
		$this->db->join('questions', 'questions.question_mcq_seq_id = chapter_masters.question_id');
		$this->db->join('question_types', 'question_types.questiontype_id = questions.question_type');
		$this->db->order_by('questions.question_type', 'ASC');
		$this->db->group_by('questions.question_type');
		$query= $this->db->get($this->db->dbprefix('chapter_masters'));
	    $result = $query->result_array();
		//echo $this->db->last_query();
		//print_r($result);
		return $result;
	}
	
	public function get_questions_detail($study_data)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		//$questiontype_id = 'SAT';
		$question_category = 'Z';

//		$sql_select = "questions.question_description,questions.question_answer_description";
				
		$sql_select = "question_mcq_seq_id, question_topic, question_type, question_description";
		if(@$_GET['q_type'])
		{
			$sql_where = "book_id = '".$study_data['book']."' AND subject_id = '".$study_data['subject']."' AND class_id = '".$study_data['class']."' AND question_type='".$_GET['q_type']."'";
		}
		else 
		{
			$sql_where = "book_id = '".$study_data['book']."' AND subject_id = '".$study_data['subject']."' AND class_id = '".$study_data['class']."'";
		}
		
		$this->db->select($sql_select);
		$this->db->where($sql_where);
		$this->db->order_by('question_mcq_seq_id', 'ASC');
		$this->db->limit(1);
		$query= $this->db->get($this->db->dbprefix('questions'));
		//echo $this->db->last_query();
	    $result = array();
		if($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result[0];
		}
		else
		{
			return "no result";
		}
	}
	
	public function filter_questions_by_type($data,$study_data,$start=NULL,$limit=NULL)
	{
		$date=date('Y-m-d H:i:s');
		
		$sql_select = '*';
		
		$this->db->select($sql_select);
		if(!($data['q_type'] == "ALL" || $data['q_type'] == ""))
		{
			$this->db->where('questions.question_type', $data['q_type']);
		}
		/*if(@$data['question_id'])
		{
			$this->db->where('questions.question_mcq_seq_id >='."'".$data['question_id']."'");
		}*/
		if(@$data['class'])
		{
			$this->db->where('questions.class_id', $data['class']);
		}
		if(@$data['subject'])
		{
			$this->db->where('questions.subject_id', $study_data['subject']);
		}
		if(@$data['book'])
		{
			$this->db->where('questions.book_id', $study_data['book']);
		}
		if(@$data['chapter'])
		{
			$this->db->where('chapter_masters.chapter_number', $study_data['chapter']);
		}
		if(@$study_data['category'] == "Z" || @$study_data['category'] == "B")
		{
			$this->db->where('chapter_masters.question_category', $study_data['category']);
		}
		
		$this->db->join('questions', 'questions.question_mcq_seq_id = chapter_masters.question_id');
		$this->db->order_by('questions.question_mcq_seq_id', 'ASC');
		if($limit==NULL || $limit=='all')
		{}
		else{
			$this->db->limit($limit);
			$this->db->offset($start);
		}
		$query= $this->db->get($this->db->dbprefix('chapter_masters'));
		//echo $this->db->last_query();
		//die();
	    $result = array();
		if($query->num_rows() > 0)
		{
			$result = $query->result_array();
			//print_r($result);
			return $result;
		}
		else
		{
			return "no result";
		}
	}
	
	public function filter_answers_by_type($data)
	{
		$date=date('Y-m-d H:i:s');
		
		$sql_select = "*";		
		
		$this->db->select($sql_select);
		$this->db->where('questions.question_mcq_seq_id', $data);
		$this->db->join('questions', 'questions.question_mcq_seq_id = chapter_masters.question_id');
		$query= $this->db->get($this->db->dbprefix('chapter_masters'));
		//echo $this->db->last_query();die();
	    $result = array();
		if($query->num_rows() > 0)
		{
			$result = $query->result_array();
//			print_r($result);
			return $result;
		}
		else
		{
			return "no result";
		}
		//print_r($result);
	}
	
	
	public function show_next4_filtered_questions($data,$study_data, $count=NULL)
	{
		$date=date('Y-m-d H:i:s');

		$sql_select = array();

		$sql_select[] = "questions.question_mcq_seq_id";
		
		if($data['q_type'] == "LAT" || $data['q_type'] == "SAT" || $data['q_type'] == "OWA")
		{
			$sql_select[] = "questions.question_description";
			$sql_select[] = "questions.question_answer_description";
		}
		else if($data['q_type'] == "MCQ" || $data['q_type'] == "TOF" || $data['q_type'] == "SEQ")
		{
			$sql_select[] = "questions.question_description";
			$sql_select[] = "questions.question_option_a";
			$sql_select[] = "questions.question_option_b";
			$sql_select[] = "questions.question_option_c";
			$sql_select[] = "questions.question_option_d";
			$sql_select[] = "questions.question_option_e";
			$sql_select[] = "questions.question_option_f";
			$sql_select[] = "questions.question_option_g";
			$sql_select[] = "questions.question_option_h";
			$sql_select[] = "questions.answer_correct_a";
			$sql_select[] = "questions.answer_correct_b";
			$sql_select[] = "questions.answer_correct_c";
			$sql_select[] = "questions.answer_correct_d";
			$sql_select[] = "questions.answer_correct_e";
			$sql_select[] = "questions.answer_correct_f";
			$sql_select[] = "questions.answer_correct_g";
			$sql_select[] = "questions.answer_correct_h";
		}
		else if($data['q_type'] == "FIB")
		{
			$sql_select[] = "questions.question_description";
			$sql_select[] = "questions.answer_a";
			$sql_select[] = "questions.answer_b";
			$sql_select[] = "questions.answer_c";
			$sql_select[] = "questions.answer_d";
			$sql_select[] = "questions.answer_e";
			$sql_select[] = "questions.answer_f";
			$sql_select[] = "questions.answer_g";
			$sql_select[] = "questions.answer_h";
		}
		else if($data['q_type'] == "MTF")
		{
			$sql_select[] = "questions.question_description";
			$sql_select[] = "questions.question_option_a";
			$sql_select[] = "questions.question_option_b";
			$sql_select[] = "questions.question_option_c";
			$sql_select[] = "questions.question_option_d";
			$sql_select[] = "questions.question_option_e";
			$sql_select[] = "questions.question_option_f";
			$sql_select[] = "questions.question_option_g";
			$sql_select[] = "questions.question_option_h";
			$sql_select[] = "questions.answer_a";
			$sql_select[] = "questions.answer_b";
			$sql_select[] = "questions.answer_c";
			$sql_select[] = "questions.answer_d";
			$sql_select[] = "questions.answer_e";
			$sql_select[] = "questions.answer_f";
			$sql_select[] = "questions.answer_g";
			$sql_select[] = "questions.answer_h";
		}
		else
		{
			$sql_select[] = "*";			
		}
				
		$this->db->select($sql_select);
		if( !($data['q_type'] == "ALL" || $data['q_type'] == ""))
		{
			$this->db->where('questions.question_type', $data['q_type']);
		}
		if(@$data['question_id'])
		{
			$this->db->where("questions.question_mcq_seq_id > '".$data['question_id']."'");
		}
		if(@$data['class'])
		{
			$this->db->where('questions.class_id', $data['class']);
		}
		if(@$data['subject'])
		{
			$this->db->where('questions.subject_id', $study_data['subject']);
		}
		if(@$data['book'])
		{
			$this->db->where('questions.book_id', $study_data['book']);
		}
		if(@$data['chapter'])
		{
			$this->db->where('chapter_masters.chapter_number', $study_data['chapter']);
		}
		if(@$study_data['category'] == "Z" || @$study_data['category'] == "B")
		{
			$this->db->where('chapter_masters.question_category', $study_data['category']);
		}
		
		$this->db->join('questions', 'questions.question_mcq_seq_id = chapter_masters.question_id');
		$this->db->order_by('questions.question_mcq_seq_id', 'ASC');
		if($count == 'all' || $count == NULL){}
		else {
			$this->db->limit($count);
		}
		$query= $this->db->get($this->db->dbprefix('chapter_masters'));
		//echo $this->db->last_query();die();
		$result = array();
		if($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result;
		}
		else
		{
			return "no result";
		}
	}
	
	
	
	#################### Flexi Pad Starts here ####################
	
	public function show_events()
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('*');
		$sql_where = "member_id = '".$member_id."'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('event_id', 'DESC')->get($this->db->dbprefix('events'));
	    $result = $query->result_array();
		//print_r($result);
		return $result;
	}
	
	public function edit_event($event_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		
		$sql_select = array('*');
		$sql_where = "event_id = '".$event_id."'";
		$query=$this->db->select($sql_select)->where($sql_where)->get($this->db->dbprefix('events'));
	    $result = $query->result_array();
		//print_r($result[0]);
		return $result[0];
	}
	
	public function add_event($data)
	{
		$date=date('Y-m-d H:i:s');
		
		$sql_insert['event_name'] = $data['eventname'];
		$sql_insert['event_detail'] = $data['eventdesc'];
		$sql_insert['event_date'] = date("Y-m-d", strtotime($data['eventdate']));
		$sql_insert['event_time'] = $data['eventtime'];
		$sql_insert['event_place'] = $data['eventplace'];
		$sql_insert['remind_before'] = $data['remindb4'];
		$sql_insert['member_id'] = $member_id;
		$sql_insert['created_date'] = $date;
		//print_r($sql_insert);
//		die;
		$query = $this->db->insert($this->db->dbprefix('events'), $sql_insert);
		if($query)
		{
			return "Success";
		}
	}
	
	public function update_event($data)
	{
		$date = date('Y-m-d H:i:s');
		
		$sql_data = array(
				'event_name' => $data['eventname'],
				'event_detail' => $data['eventdesc'],
				'event_date' => date("Y-m-d", strtotime($data['eventdate'])),
				'event_time' => $data['eventtime'],
				'event_place' => $data['eventplace'],
				'remind_before' => $data['remindb4'],
//				'member_id' => $member_id,
				'modified_date' => $date
            );
		$this->db->where('event_id', $data['event_id']);
		$query = $this->db->update($this->db->dbprefix('events'), $sql_data); 
		if($query)
		{
			return "Success";
		}
	}
	
	
	
	#################### Flexi Pad Ends here ######################

	
	
	#################### Jagdish's demo model code starts here ######################

	public function create_event_model($data){
		$date=date('Y-m-d H:i:s');
		
		$sql_insert['event_name'] = $data['eventname'];
		$sql_insert['event_detail'] = $data['eventdesc'];
		$sql_insert['event_date'] = date("Y-m-d", strtotime($data['eventdate']));
		$sql_insert['event_time'] = $data['eventtime'];
		$sql_insert['event_place'] = $data['eventplace'];
		$sql_insert['remind_before'] = $data['remindb4'];
		$sql_insert['member_id'] = $member_id;
		$sql_insert['created_date'] = $date;
		//print_r($sql_insert);
//		die;
		$query = $this->db->insert($this->db->dbprefix('events'), $sql_insert);
		if($query)
		{
			return "Success";
		}
	}
	
/*	public function chart_data($question){
		$this->db->select('question_mcq_seq_id as ques, COUNT(question_mcq_seq_id) as total');
		$this->db->select('Year(created_date) as year');
		$this->db->group_by('Year(created_date)');
		$this->db->where('question_mcq_seq_id',$q_id);
		$query = $this->db->get('zrc_question_access_log');
		return $query->result_array();
	}
*/
	
	public function sharing_entry($share){
		$data = array();
		$data['question_mcq_seq_id'] = $share['question_id'];
		$data['member_id'] = '1';
		$data['question_shared_platform'] = $share['share']; 
		$data['created_date'] = date('y:m:d h:i:s');
		$query = $this->db->insert('zrc_question_shared_log',$data);
		
		if($query){
			$this->db->where('question_mcq_seq_id',$data['question_mcq_seq_id']);
			$q = $this->db->get('zrc_question_analytics');
			$nums = $q->num_rows();
			$result = $q->result_array();
			if($nums > 0)
			{
				$data1 = array();
				$question_shared_count = 1;
				if($share=='facebook'){
					$this->db->set('question_fb_count','question_fb_count+1',FALSE);
				}
				if($share=='twitter'){
					$this->db->set('question_twitted_count','question_twitted_count+1',FALSE);
				}
				if($share=='googleplus'){
					$this->db->set('question_goolgeplus_count','question_goolgeplus_count+1',FALSE);
				}
				if($share=='linkedin'){
					$this->db->set('question_linkedin_count','question_linkedin_count+1',FALSE);
				}
				if($share=='blogger'){
					$this->db->set('question_blogger_count','question_blogger_count+1',FALSE);
				}
				$this->db->set('question_shared_count', 'question_shared_count+1',FALSE);
				$this->db->where('question_mcq_seq_id',$data['question_mcq_seq_id']);
				$this->db->update('zrc_question_analytics');
			}
			return true;
		}
		else {
			return false;
		}
	}
		
/*	public function question_analytics($value){
		$q='q1';
	//	$this->db->select('');
		$this->db->where('question_mcq_seq_id',$q);
		$query = $this->db->get('zrc_question_analytics');
		return $query->result_array();
	}
*/	
	public function member_question_upload_model($value){
		$data = array();
		$data['member_id'] = '1';
		$data['question_reference'] = '2';
		$data['question'] = $value['questions'];
		$data['answer'] = $value['answers'];
		$query = $this->db->insert('zrc_member_question_upload',$data);
		if($query){
			return 'Success';
		}else{
			return 'Failure';
		}
	}

	public function add_question_tip($data, $que_id)
	{
		$date=date('Y-m-d H:i:s');
		
		$sql_insert['question_mcq_seq_id'] = $que_id;
		$sql_insert['question_tip'] = $data['question_tip'];
		//$sql_insert['question_tip'] = $data;
		$sql_insert['created_by'] = $member_id;
		$sql_insert['created_date'] = $date;
		
		$query = $this->db->insert($this->db->dbprefix('question_tips'), $sql_insert);
		if($query)
		{
			return "Success";
		}
	}
	
	
	
	public function user_email_check_feedback(){
		$this->db->where('email',$this->input->post('email'));
		$query = $this->db->get('zrc_members');
		$num_rows = $query->num_rows();
		if($num_rows>0){
			$result = $query->result_array();
			$this->session->set_userdata('feedback_user_insert_id',$result[0]['member_id']); 
			return true;
		}
	}
	
	public function register_user_feedback($value){
		$data = array();
		$da = explode(' ', $value['name'],2); 
		if(count($da)>1){
			$data['first_name'] = $da[0];
			$data['last_name'] = $da[1];
		}else {
			$data['first_name'] = $value['name'];
		}
		$data['email'] = $value['email'];
		$data['active'] = 'N';
		$data['created_date'] = date('Y-m-d H:i:s');
		$query = $this->db->insert('zrc_members',$data);
		if($query){
			 $insert_id = $this->db->insert_id();
			 $this->session->set_userdata('feedback_user_insert_id',$insert_id);
			return true;
		}
		else {
			return false;
		}
	}
	
	public function insert_feedback($feedback,$category){
		$data = array();
		
//			$data['feedback_category_id'] = $category;
			$data['feedback_category_id'] = 1;
			$data['feedback_description'] = $feedback;
			$data['feedback_submitted_by'] = $this->session->userdata('feedback_user_insert_id');
			$data['feedback_status'] = 'Not Replied';
			$data['creation_date'] = date('Y-m-d H:i:s');
			$query = $this->db->insert('zrc_user_feedbacks',$data);
			if($query){
				return true;
			}else {
				return false;
			}
	}

	#################### Jagdish's demo controller code ends here ######################
	
	
	public function set_question_note($question_id)
	{
		$member_id = $this->session->userdata('id');
		$date=date('Y-m-d H:i:s');
		
		$sql_select1 = array('question_notes','question_bookmark');
		$this->db->select($sql_select1);
		$this->db->where('member_id', $member_id);
		if(@$question_id)
		{
			$this->db->where('question_mcq_seq_id', $question_id);
		}
		$query = $this->db->get($this->db->dbprefix('question_notes'));	
		$result = $query->result_array();
		//echo $this->db->last_query();die();
		$question_notes = array();
		if(($query->num_rows() > 0) && @$question_id)
		{
			$question_notes['question_note'] = $result[0]['question_notes'];
			$question_notes['question_bookmark_status'] = $result[0]['question_bookmark'];
			return $question_notes;
		}
		
	}
	
	public function set_question_liked_status($question_id)
	{
		$member_id = $this->session->userdata('id');
		$date=date('Y-m-d H:i:s');
		$sql_select1 = array('question_like_status');
		$this->db->select($sql_select1);
		$this->db->where('member_id', $member_id);
		if(@$question_id)
		{
			$this->db->where('question_mcq_seq_id', $question_id);
		}		
		$query = $this->db->get($this->db->dbprefix('question_like_log'));	
		$result = $query->result_array();
		if(($query->num_rows() > 0) && @$question_id)
		{
			return $result[0]['question_like_status'];
		}
		else
		{
			return "no result";
		}
		
	}
	
	public function set_question_note_or_bookmark($question_id, $data)
	{
		$member_id = $this->session->userdata('id');
		$date=date('Y-m-d H:i:s');

		$sql_select1 = array('*');
		$this->db->select($sql_select1);
		$this->db->where('member_id', $member_id);
		if(@$question_id)
		{
			$this->db->where('question_mcq_seq_id', $question_id);
		}
		$query1= $this->db->get($this->db->dbprefix('question_notes'));
		$result = $query1->result_array();
		if(@$data['note'])
		{
			$note = $data['note'];
		}else {
			$note = $result[0]['question_notes'];
		}
		if( ! @$data['type'])
		{
			if($result[0]['question_bookmark'] == 'Yes')
			{
				$bookmark = 'No';							
			}
			else 
			{
				$bookmark = 'Yes';							
			}
		}
		else 
		{
			$bookmark = $result[0]['question_bookmark'];			
		}
		
		if(!$query1->num_rows() > 0)
		{
			$sql_insert['question_mcq_seq_id'] = $question_id;
			$sql_insert['member_id'] = $member_id;
			$sql_insert['question_bookmark'] = $bookmark;
			$sql_insert['question_notes'] = $note;
			$sql_insert['created_date'] = $date;
			$query = $this->db->insert($this->db->dbprefix('question_notes'), $sql_insert);
		}
		else
		{
			$query = $this->db->query("UPDATE ". $this->db->dbprefix('question_notes') ." SET question_bookmark = '".$bookmark."', question_notes = '".$note."', modified_date = '".$date."', modified_by = ".$member_id." WHERE member_id=". $member_id ." AND question_mcq_seq_id='". $question_id ."'");
		}
		if($query)
		{
			$sql_select2 = array('question_notes');
			$this->db->select($sql_select2);
			$this->db->where('member_id', $member_id);
			if(@$question_id)
			{
				$this->db->where('question_mcq_seq_id', $question_id);
			}
			$query2= $this->db->get($this->db->dbprefix('question_notes'));
			$result2 = $query2->result_array();
			return $result2[0]['question_notes'];
		}
		
	}
	
	public function question_print($question_id, $data)
	{
		$member_id = $this->session->userdata('id');
		$date=date('Y-m-d H:i:s');
		
		
		$sql_insert['question_mcq_seq_id'] = $question_id;
		$sql_insert['member_id'] = $member_id;
		$sql_insert['created_date'] = $date;
		$query = $this->db->insert($this->db->dbprefix('question_print_log'), $sql_insert);
		if($query)
		{
			return "Success";
		}		
	}
	
	public function get_question_rate($question_id)
	{
		$member_id = $this->session->userdata('id');
		$date=date('Y-m-d H:i:s');
		
		$sql_select1 = 'AVG(question_rating) as question_rating';
		$this->db->select($sql_select1);
		//$this->db->where('member_id', $member_id);
		if(@$question_id)
		{
			$this->db->where('question_mcq_seq_id', $question_id);
		}
		$query1= $this->db->get($this->db->dbprefix('question_rating_log'));
		$result = $query1->result_array();
		if(($query1->num_rows() > 0) && @$question_id)
		{
			return $result[0]['question_rating'];
		}
		else
		{
			return "no result";
		}
		
	}
	
	public function get_question_flag($question_id)
	{
		$member_id = $this->session->userdata('id');
		$date=date('Y-m-d H:i:s');
		
		$sql_select1 = 'question_flag_type';
		$this->db->select($sql_select1);
		$this->db->where('member_id', $member_id);
		if(@$question_id)
		{
			$this->db->where('question_mcq_seq_id', $question_id);
		}
		$query1= $this->db->get($this->db->dbprefix('question_flag_log'));
		$result = $query1->result_array();
		if(($query1->num_rows() > 0) && @$question_id)
		{
			return $result[0]['question_flag_type'];
		}
		else
		{
			return "no result";
		}
		
	}
	
	public function rate_this_question($question_id, $data)
	{
		$member_id = $this->session->userdata('id');
		$date=date('Y-m-d H:i:s');
		
		$sql_select1 = array('*');
		$this->db->select($sql_select1);
		$this->db->where('member_id', $member_id);
		if(@$question_id)
		{
			$this->db->where('question_mcq_seq_id', $question_id);
		}
		$query1= $this->db->get($this->db->dbprefix('question_rating_log'));
		if( ! $query1->num_rows() > 0)
		{			
			$sql_insert['question_mcq_seq_id'] = $question_id;
			$sql_insert['member_id'] = $member_id;
			$sql_insert['question_rating'] = $data['rating'];
			$sql_insert['created_date'] = $date;
			$query = $this->db->insert($this->db->dbprefix('question_rating_log'), $sql_insert);
		}
		else
		{
			$query = $this->db->query("UPDATE ". $this->db->dbprefix('question_rating_log') ." SET question_rating = '".$data['rating']."' WHERE member_id=". $member_id ." AND question_mcq_seq_id='". $question_id ."'");			
		}
		if($query)
		{
			return "Success";
		}
	}
	

	
	public function get_questionanalytics_access_counts($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$sql_select = array('*');
		$sql_where = "question_mcq_seq_id = '". $question_id ."'";
		$query=$this->db->select($sql_select)->where($sql_where)->get($this->db->dbprefix('question_access_log'));
	    return $query->num_rows();
	}
	
	public function get_questionanalytics_access_counts_graph_weekly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$sql_select = array('created_date', 'COUNT(WEEK(created_date)) as total_counts', 'WEEK(created_date) as weeks');
		$sql_where = "question_mcq_seq_id = '". $question_id ."'";
		$query=$this->db->select($sql_select)->where($sql_where)->group_by('WEEK(created_date)')->order_by('WEEK(created_date)', 'DESC')->limit(4)->get($this->db->dbprefix('question_access_log'));
	    $result = $query->result_array();
		//print_r($result);
		return $result;
	}
		
	public function get_questionanalytics_access_counts_graph_monthly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$sql_select = array('created_date', 'COUNT(MONTH(created_date)) as total_counts', 'MONTH(created_date) as months');
		$sql_where = "question_mcq_seq_id = '". $question_id ."'";
		$query=$this->db->select($sql_select)->where($sql_where)->group_by('MONTH(created_date)')->order_by('MONTH(created_date)', 'DESC')->limit(4)->get($this->db->dbprefix('question_access_log'));
	    $result = $query->result_array();
		//print_r($result);
		return $result;
	}
		
	public function get_questionanalytics_liked_counts($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$sql_select = array('*');
		$sql_where = "question_mcq_seq_id = '". $question_id ."' AND question_like_status='Yes'";
		$query=$this->db->select($sql_select)->where($sql_where)->get($this->db->dbprefix('question_like_log'));
	    return $query->num_rows();
	}

	public function get_questionanalytics_liked_counts_graph_weekly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$sql_select = array('created_date', 'COUNT(WEEK(created_date)) as total_counts', 'WEEK(created_date) as weeks');
		$sql_where = "question_mcq_seq_id = '". $question_id ."' AND question_like_status='Yes'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('WEEK(created_date)', 'DESC')->group_by('WEEK(created_date)')->limit(4)->get($this->db->dbprefix('question_like_log'));
	    $result = $query->result_array();
		return $result;
	}
	
	public function get_questionanalytics_liked_counts_graph_monthly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$sql_select = array('created_date', 'COUNT(MONTH(created_date)) as total_counts', 'MONTH(created_date) as months');
		$sql_where = "question_mcq_seq_id = '". $question_id ."' AND question_like_status='Yes'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('MONTH(created_date)', 'DESC')->group_by('MONTH(created_date)')->limit(4)->get($this->db->dbprefix('question_like_log'));
	    $result = $query->result_array();
		return $result;
	}
	
	
	public function get_questionanalytics_shared_counts($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$sql_select = array('question_shared_platform', 'count(question_shared_platform) as shared_count');
		$sql_where = "question_mcq_seq_id = '". $question_id ."'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('question_shared_platform', 'ASC')->group_by('question_shared_platform')->get($this->db->dbprefix('question_shared_log'));
	    $result = $query->result_array();
		return $result;
	}
	
	public function get_questionanalytics_shared_counts_graph_weekly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$sql_select = array('SUM(IF(`question_shared_platform` = "twitter",1,0)) as twitter', 'SUM(IF(`question_shared_platform` = "linkedin",1,0)) as linkedin', 'SUM(IF(`question_shared_platform` = "facebook",1,0)) as facebook', 'WEEK(created_date) as weeks');
		$sql_where = "question_mcq_seq_id = '". $question_id ."'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('WEEK(created_date)', 'DESC')->group_by('WEEK(created_date)')->limit(4)->get($this->db->dbprefix('question_shared_log'));

	    $result = $query->result_array();
		return $result;
	}
	
	public function get_questionanalytics_shared_counts_graph_monthly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$sql_select = array('SUM(IF(`question_shared_platform` = "twitter",1,0)) as twitter', 'SUM(IF(`question_shared_platform` = "linkedin",1,0)) as linkedin', 'SUM(IF(`question_shared_platform` = "facebook",1,0)) as facebook', 'MONTH(created_date) as months');
		$sql_where = "question_mcq_seq_id = '". $question_id ."'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('MONTH(created_date)', 'DESC')->group_by('MONTH(created_date)')->limit(4)->get($this->db->dbprefix('question_shared_log'));

	    $result = $query->result_array();
		return $result;
	}
	
	public function get_questionanalytics_flag_counts($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$sql_select = array('question_flag_type', 'count(question_flag_type) as flag_count');
		$sql_where = "question_mcq_seq_id = '". $question_id ."'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('question_flag_type', 'ASC')->group_by('question_flag_type')->get($this->db->dbprefix('question_flag_log'));
	    $result = $query->result_array();
		return $result;
	}
	
	public function get_questionanalytics_flag_counts_graph_weekly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$sql_select = array('created_date', 'COUNT(WEEK(created_date)) as total_counts', 'WEEK(created_date) as weeks');
		$sql_where = "question_mcq_seq_id = '". $question_id ."'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('WEEK(created_date)', 'DESC')->group_by('WEEK(created_date)')->limit(4)->get($this->db->dbprefix('question_flag_log'));
	    $result = $query->result_array();
		return $result;
	}
	
	public function get_questionanalytics_flag_counts_graph_monthly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$sql_select = array('created_date', 'COUNT(MONTH(created_date)) as total_counts', 'MONTH(created_date) as months');
		$sql_where = "question_mcq_seq_id = '". $question_id ."'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('MONTH(created_date)', 'DESC')->group_by('MONTH(created_date)')->limit(4)->get($this->db->dbprefix('question_flag_log'));
	    $result = $query->result_array();
		return $result;
	}

	public function get_questionanalytics_download_counts($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$sql_select = array('*');
		$sql_where = "question_mcq_seq_id = '". $question_id ."'";
		$query=$this->db->select($sql_select)->where($sql_where)->get($this->db->dbprefix('question_download_log'));
	    $result = $query->num_rows();
		return $result;
	}
	
	public function get_questionanalytics_download_counts_graph_weekly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$sql_select = array('created_date', 'COUNT(WEEK(created_date)) as total_counts', 'WEEK(created_date) as weeks');
		$sql_where = "question_mcq_seq_id = '". $question_id ."'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('WEEK(created_date)', 'DESC')->group_by('WEEK(created_date)')->limit(4)->get($this->db->dbprefix('question_download_log'));
	    $result = $query->result_array();
		return $result;
	}
	
	public function get_questionanalytics_download_counts_graph_monthly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$que_id = $question_id;
		$sql_select = array('created_date', 'COUNT(MONTH(created_date)) as total_counts', 'MONTH(created_date) as months');
		$sql_where = "question_mcq_seq_id = '".$que_id."'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('MONTH(created_date)', 'DESC')->group_by('MONTH(created_date)')->limit(4)->get($this->db->dbprefix('question_download_log'));
	    $result = $query->result_array();
		return $result;
	}
	
	public function get_questionanalytics_time_to_answer($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$sql_select = array('FORMAT(AVG(`question_correct_answer_time`), 2) as avgtime');
		$sql_where = "question_mcq_seq_id = '". $question_id ."' AND question_correct_answer > 0";
		$query=$this->db->select($sql_select)->where($sql_where)->get($this->db->dbprefix('question_answer_log'));
	    $result = $query->result();
		echo $result[0]->avgtime;
	}
	
	public function get_questionanalytics_time_to_answer_graph_weekly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$sql_select = array('created_date', 'question_correct_answer_time', 'FORMAT(SUM(`question_correct_answer_time`), 2) as correctanswertime', 'FORMAT(SUM(`question_wrong_answer_time`), 2) as wronganswertime', 'WEEK(created_date) as weeks');
		$sql_where = "question_mcq_seq_id = '". $question_id ."'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('WEEK(created_date)', 'DESC')->group_by('WEEK(created_date)')->limit(4)->get($this->db->dbprefix('question_answer_log'));
	    $result = $query->result_array();
		return $result;
	}
	
	public function get_questionanalytics_time_to_answer_graph_monthly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$sql_select = array('created_date', 'question_correct_answer_time', 'FORMAT(SUM(`question_correct_answer_time`), 2) as correctanswertime', 'FORMAT(SUM(`question_wrong_answer_time`), 2) as wronganswertime', 'MONTH(created_date) as months');
		$sql_where = "question_mcq_seq_id = '". $question_id ."'";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('MONTH(created_date)', 'DESC')->group_by('MONTH(created_date)')->limit(4)->get($this->db->dbprefix('question_answer_log'));
	    $result = $query->result_array();
		return $result;
	}
	
	public function get_questionanalytics_correct_answer_counts($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		$sql_select = array('*');
		$sql_where = "question_mcq_seq_id = '". $question_id ."' AND question_correct_answer = 1";
		$query=$this->db->select($sql_select)->where($sql_where)->get($this->db->dbprefix('question_answer_log'));
	    $result = $query->num_rows();
		return $result;
	}
	
	public function get_questionanalytics_correct_answer_counts_graph_weekly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		$sql_select = array('created_date', 'COUNT(WEEK(created_date)) as total_counts', 'WEEK(created_date) as weeks');
		$sql_where = "question_mcq_seq_id = '". $question_id ."' AND question_correct_answer = 1";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('WEEK(created_date)', 'DESC')->group_by('WEEK(created_date)')->limit(4)->get($this->db->dbprefix('question_answer_log'));
	    $result = $query->result_array();
		return $result;
	}
	
	public function get_questionanalytics_correct_answer_counts_graph_monthly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		$sql_select = array('created_date', 'COUNT(MONTH(created_date)) as total_counts', 'MONTH(created_date) as months');
		$sql_where = "question_mcq_seq_id = '". $question_id ."' AND question_correct_answer = 1";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('MONTH(created_date)', 'DESC')->group_by('MONTH(created_date)')->limit(4)->get($this->db->dbprefix('question_answer_log'));
	    $result = $query->result_array();
		return $result;
	}
	

	public function get_questionanalytics_wrong_answer_counts($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		$que_id = $question_id;
		$sql_select = array('*');
		$sql_where = "question_mcq_seq_id = '". $question_id ."' AND question_wrong_answer = 1";
		$query=$this->db->select($sql_select)->where($sql_where)->get($this->db->dbprefix('question_answer_log'));
	    $result = $query->num_rows();
		return $result;
	}
	
	
	public function get_questionanalytics_wrong_answer_counts_graph_weekly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		$que_id = $question_id;
		$sql_select = array('created_date', 'COUNT(WEEK(created_date)) as total_counts', 'WEEK(created_date) as weeks');
		$sql_where = "question_mcq_seq_id = '". $question_id ."' AND question_wrong_answer = 1";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('WEEK(created_date)', 'DESC')->group_by('WEEK(created_date)')->get($this->db->dbprefix('question_answer_log'));
	    $result = $query->result_array();
		return $result;
	}
	
	public function get_questionanalytics_wrong_answer_counts_graph_monthly($question_id)
	{
		$date=date('Y-m-d H:i:s');
		$member_id = $this->session->userdata('id');
		$que_id = $question_id;
		$sql_select = array('created_date', 'COUNT(MONTH(created_date)) as total_counts', 'MONTH(created_date) as months');
		$sql_where = "question_mcq_seq_id = '". $question_id ."' AND question_wrong_answer = 1";
		$query=$this->db->select($sql_select)->where($sql_where)->order_by('MONTH(created_date)', 'DESC')->group_by('MONTH(created_date)')->get($this->db->dbprefix('question_answer_log'));
	    $result = $query->result_array();
		return $result;
	}
	
	
	
####################### 	Analytics, Que Share and Floater bar code ends here   #############################

	
	
}
?>